import { getDetailUserInfoApi, getInfoApi, loginApi } from '@/api/user'

import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  // 开启模块化
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    setToken(state, data) {
      state.token = data

      // 存入cookie实现持久化
      setToken(data)
      console.log(data)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    // 设置用户资料
    setUserinfo(state, data) {
      state.useInfo = data
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async loginAction(context, data) {
      const res = await loginApi(data)
      // console.log(data)
      context.commit('setToken', res)
      console.log(res)
    },
    async getUserInfoAction(context) {
      const res = await getInfoApi()
      const res2 = await getDetailUserInfoApi(res.userId)
      console.log(res2)
      context.commit('setUserInfo', { ...res, ...res2 })
    },
    //退出操作
    quitAction(context) {
      //清除token和用户资料
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }
  }
}
